package ru.urfc.portal.developer_tasks.ejb_tests;

import javax.ejb.EJB;
import javax.ejb.EJBTransactionRolledbackException;

import ru.urfc.portal.dao.BasePersistence;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.NotificationSettings;

public class ClientNotifificationSettingsTestEjb extends BaseTestEjb {

	@EJB BasePersistence basePersistence;

	Branch branchNorth;
	
	@Override
	protected void setUp() throws Exception {
		branchNorth = exactlyOne(find(Branch.class, "code", Branch.SVERD.getCode()));
	}
	
	public void testPersistOk() {
		Client client = persist(new Client(null, 1123L, branchNorth, true, true));
		client.setNotificationSettings(new NotificationSettings(client, true, true, true, 0, true, 1, 2));
		exactlyOne(find(NotificationSettings.class, "client.id", client.getId()));
	}

	public void testPersistFailed() {
		Client client = persist(new Client(null, 1123L, branchNorth, true, true));
		try {
			assertEquals(0, find(NotificationSettings.class, "client.id", client.getId()).size());// force INSERT
			fail("exception expected");
		} catch (EJBTransactionRolledbackException e) {
			// ok
		}
	}
	
	public void testDelete() {
		Client client = persist(new Client(null, 1123L, branchNorth, true, true));
		client.setNotificationSettings(new NotificationSettings(client, true, true, true, 0, true, 1, 2));
		assertEquals(1, find(NotificationSettings.class, "client.id", client.getId()).size());
		delete(client);
		assertEquals(0, find(NotificationSettings.class, "client.id", client.getId()).size());
	}

}
